<?php
namespace App\Http\Controllers\Admin;
//店铺信息Brand审核

use Illuminate\Http\Request;  
use App\Shop; 
use App\BBrand;
use App\Brand;
use App\Services\MyLog;

class BrandShenheController extends AdminController
{
    // 列表
    public function alist(Request $request)
    { 
        $status = trim($request->input('status', ''));
        $category_id = trim($request->input('category_id', ''));//业态
        $shop_id = trim($request->input('shop_id', ''));
        $shop_name = trim($request->input('shop_name', ''));
        
        
        $cn_name = trim($request->input('cn_name', ''));
        
        $fliter = BBrand::where('shop_id', '>', 0);
        
        
        if($status){
            $fliter = $fliter->where('shenhe_status', $status);
        }
        if($category_id){
            $fliter =  $fliter->whereIn('shop_id', Shop::where('category_id', $category_id)->pluck('member_id'));
        }
        if($shop_id){
            $fliter =  $fliter->where('shop_id', $shop_id);
        }
        if($shop_name){
            $fliter =  $fliter->whereIn('shop_id', Shop::where('company', 'like', "%$shop_name%")->pluck('member_id'));
        }
        
        if($cn_name){
            $fliter =  $fliter->where('name', 'like', "%$cn_name%");
        }
         
        $this->itemsWithPage($request, $fliter);  
    }
    
    public function shenhe(Request $request){
        $status = trim($request->input('status', ''));//2:审核通过,3:审核不通过
        $status_extra = trim($request->input('status_extra', ''));
        $id = $request->input('id');//2:审核通过,3:审核不通过
        $baseinfo = BBrand::where('id', $id)->first();
        if(!$baseinfo){
            $this->err('id错误');
        }
        if($baseinfo->status != 1){
            $this->err('已审核不能再次审核');
        }
        $baseinfo->shenhe_status = $status;
        $baseinfo->shenhe_extra = $status_extra; 
        if($baseinfo->save()){ 
            if($baseinfo->shenhe_status == 2){  
                $brandReal = false;
                if($baseinfo->realid){
                    $brandReal = Brand::where('id', $baseinfo->realid)->first();
                }
                if(!$brandReal){
                    $brandReal = new Brand(); 
                }
                $brandReal->name = $baseinfo->name;
                $brandReal->logo = $baseinfo->logo;
                $brandReal->weight = $baseinfo->weight;
                $brandReal->status = 1;
                $brandReal->hits = $baseinfo->hits;
                $brandReal->english_name = $baseinfo->english_name;
                $brandReal->catid = $baseinfo->catid ;
                $brandReal->shop_id = $baseinfo->shop_id;
                $brandReal->discription = $baseinfo->discription;
                $brandReal->xuke = $baseinfo->xuke;
                if(!$brandReal->save()){ 
                    $this->err('审核错误'); 
                } 
                $baseinfo->realid = $brandReal->id;
                $baseinfo->save();
            }
        }
        $this->output(array(), '审核完成');
    }
}